//
//  FactoryDetailTableViewCell.m
//  Rail
//
//  Created by hychen on 2018/3/5.
//  Copyright © 2018年 Hychen. All rights reserved.
//

#import "FactoryDetailTableViewCell.h"
#import "BMItemView.h"

@interface FactoryDetailTableViewCell ()
@property (nonatomic, strong) UIView *containerView;
@property (nonatomic, strong) BMLineView *lineView;
@end
@implementation FactoryDetailTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.lineView = [BMLineView defaultLineView];
        [self.contentView addSubview:self.lineView];
        [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(self.contentView);
            make.height.mas_equalTo(10);
        }];
        
        self.containerView = [UIView new];
        [self.contentView addSubview:self.containerView];
        [self.containerView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.right.equalTo(self.contentView);
            make.bottom.equalTo(self.lineView.mas_top);
        }];
    }
    return self;
}

- (void)setupItemViews {
    [self.containerView removeAllSubviews];
    UIView *lastView = nil;
    for (int i = 0; i < self.dataArray.count; ++i) {
        BMItemModel *model = [self.dataArray objectAtIndex:i];
        BMItemView *view = [[BMItemView alloc] init];
        view.index = i+1;
        BMWeakSelf
        [view setBlockClickItem:^(BMItemModel *model, NSInteger index) {
            if (weakSelf.BlockClickItem) {
                weakSelf.BlockClickItem(model, index);
            }
        }];
        [self.containerView addSubview:view];
        if (!lastView) {
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.top.bottom.equalTo(self.containerView);
            }];
        }
        else {
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(lastView.mas_right);
                make.width.equalTo(lastView);
                make.top.bottom.equalTo(lastView);
                if (i == self.dataArray.count-1) {
                    make.right.equalTo(self.containerView);
                }
            }];
        }
        [view setModel:model];
        lastView = view;
    }
}

- (void)setDataArray:(NSArray *)dataArray {
    _dataArray = dataArray;
    [self setupItemViews];
}

- (UIView *)containerView
{
    if (!_containerView) {
        _containerView = [UIView new];
    }
    return _containerView;
}
@end
